package com.qk.management.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.common.constant.BusinessStatusConstants;
import com.qk.domain.business.BusinessDO;
import com.qk.dto.business.BusinessDTO;
import com.qk.dto.business.BusinessPoolDTO;
import com.qk.dto.business.BusinessQueryDTO;
import com.qk.dto.business.UpdateBusinessInfoDTO;
import com.qk.entity.Business;
import com.qk.management.service.BusinessService;
import com.qk.vo.BusinessVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@Slf4j
@RestController
@RequestMapping("/businesses")
@SuppressWarnings("all")
public class BusinessController {
    @Autowired
    private BusinessService businessService;

    @PostMapping("/toCustomer/{id}")
    public Result convertToCustomer(@PathVariable Integer id){
        log.info("商机id为：{}，转换成客户", id);
        businessService.convertToCustomer(id);
        return Result.success();
    }
    @GetMapping("pool")
    public Result pool(BusinessPoolDTO dto){
        log.info("查询商机池");
        PageResult<BusinessDO> list = businessService.selectBusinessPoolPage(dto);
        return Result.success(list);
    }
    @PutMapping("/back/{id}")
    public Result back(@PathVariable Integer id){
        log.info("回退商机id为：{}", id);
        businessService.back(id);
        return Result.success();
    }
    @PutMapping
    public Result updateBusinessInfo( @RequestBody UpdateBusinessInfoDTO dto){
        log.info("跟进商机：{}", dto);
        businessService.updateBusinessInfoById(dto);
        return Result.success();
    }
    @GetMapping("/{id}")
    public Result getBusinessInfoById(@PathVariable Integer id){
        log.info("查询id为：{}的商机", id);
        BusinessVO vo = businessService.getBusinessInfoById(id);
        return Result.success(vo);
    }

    @PostMapping
    public Result addBusiness(@RequestBody BusinessDTO dto){
        log.info("添加商机：{}", dto);
        businessService.addBusiness(dto);
        return Result.success();
    }


    @PutMapping("/assign/{businessId}/{userId}")
    public Result assign(@PathVariable Integer businessId, @PathVariable Integer userId) {
        log.info("分配商机id为：{}，用户id为：{}", businessId, userId);
        businessService.updateById(Business.builder()
                        .id(businessId)
                        .userId(userId)
                        .updateTime(LocalDateTime.now())
                        .status(BusinessStatusConstants.WAIT_FOLLOW_UP)
                        .build());
        return Result.success();
    }

    @GetMapping
    public Result page(BusinessQueryDTO dto) {
        log.info("查询参数：{}", dto);
        PageResult<BusinessDO> page = businessService.selectPage(dto);
        return Result.success(page);
    }
}
